package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bean.vo.BankBalanceVO;
import com.jishuo.nontax.bfc.service.BankBalanceService;
import org.dom4j.Element;

import java.math.BigDecimal;

/**
 * @author hlp
 * @date 2023/2/8 15:16
 * @Description:
 */
public class BankBalanceApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if(head.equals("setBankBalance")){  //  记录账户余额信息
            BankBalanceVO bankBalanceVO = new BankBalanceVO();

            bankBalanceVO.setAccountcode(body.elementText("accountcode"));
            bankBalanceVO.setAccountdate(body.elementText("accountdate"));
            bankBalanceVO.setFund(new BigDecimal(body.elementText("fund")));

            if(bankBalanceVO.getAccountcode()==null||bankBalanceVO.getAccountcode().equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountcode]");
            }

            BankBalanceService bankBalanceService = (BankBalanceService) SpringContextUtil.getBean("bankBalanceService");
            BaseResult ret = bankBalanceService.setBankBalance(bankBalanceVO);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }else if(head.equals("sendBankBalance")){ // 向财政发送账户信息
            String accountcode = body.elementText("accountcode");
            String accountdate = body.elementText("accountdate");
            if(accountcode==null||accountcode.equals("")){
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数为空[accountcode]");
            }
            BankBalanceService bankBalanceService = (BankBalanceService) SpringContextUtil.getBean("bankBalanceService");
            BaseResult ret = bankBalanceService.sendBankBalance(accountcode,accountdate);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>"+n+"</returninfo>");
            return ret;
        }
        return null;
    }
}
